<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { loginApi } from "@/api/login";
import { useRouter } from "vue-router";

const router = useRouter();

// 登录数据
const loginForm = ref({
  username: "",
  password: "",
});

// 登录
const handleLogin = async () => {
  const result = await loginApi(loginForm.value);
  if (result.code) {
    ElMessage.success("登录成功");
    // 存储服务端响应的数据 token,登录用户的信息 - localStorage
    localStorage.setItem("token", result.data.token);
    localStorage.setItem("userInfo", JSON.stringify(result.data));
    // 跳转到首页 - 路由
    router.push("/home");
  } else {
    ElMessage.error(result.msg);
  }
};
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="system-title">轻客管家</div>
      <el-form :model="loginForm" label-width="80px" class="login-form">
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="handleLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("@/assets/bg.jpg") center center / cover no-repeat;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: "";
  position: absolute;
  width: 200%;
  height: 200%;
  top: -50%;
  left: -50%;
  background: radial-gradient(
    circle,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0) 60%
  );
  animation: rotate 30s linear infinite;
}

.system-title {
  font-family: 华文隶书;
  font-size: 50px;
  text-align: center;
  margin-bottom: 30px;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.login-box {
  width: 450px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 1;
}

.login-form {
  width: 100%;
}

/* 确保表单项标签和输入框完美对齐 */
.el-form-item {
  margin-bottom: 20px;
}

.el-form-item__label {
  font-weight: 500;
  color: #333;
  text-align: right;
  padding-right: 12px;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}

.el-input {
  width: 100%;
}

.el-input__wrapper {
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
}

.el-input__wrapper:hover {
  border-color: #1890ff;
}

.el-input__wrapper.is-focus {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.el-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background: linear-gradient(90deg, #1890ff, #36cfc9);
  border: none;
  transition: transform 0.3s ease;
  border-radius: 8px;
}

.el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.login-btn {
  width: 100%;
  margin-top: 10px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    width: 90%;
    padding: 30px 20px;
  }

  .system-title {
    font-size: 36px;
  }
}
</style>
